/**
 * 用户注册信息类
 * 用于新用户注册(注册用户+企业信息)
 * @author lms 2022.07.30
 */
package ms.core.gateway.request;

import java.util.List;
import java.util.Map;

import lombok.Data;
import ms.core.common.AppException;
import ms.core.common.consts.SysErr;
import ms.core.gateway.bean.SysCorpBean;
import ms.core.gateway.response.DicConstVO.ConstItem;
import ms.core.gateway.tool.SessionTool;
import ms.core.gateway.utils.DicConstUtils;
import ms.core.tool.JsonTool;
import ms.core.tool.RegularTool;
import ms.core.tool.StrTool;
import ms.core.tool.RegularTool.CheckResult;

@Data
public class RegUserRequest {
	private String corpName;	// 企业名称
	private int ctype;			// 企业类型(2的幂次方，>=1类型企业,根据实际应用定义)
	private String account;		// 帐号
	private String password;	// 密码
	private String code;		// 验证码
	private String mobile;		// 手机号
	
	public static RegUserRequest getInstance(Map<String, Object> body, String sid) {
		RegUserRequest usr = JsonTool.objectToBean(body, RegUserRequest.class);

		if (!usr.getCode().equalsIgnoreCase(SessionTool.getCheckCode(sid)))
			throw new AppException(SysErr.SYS_CHECKCODE_ERR);

		// 验证帐号(邮箱)有效性
		CheckResult cr = RegularTool.checkValue("email", usr.getAccount());
		if (!cr.getResult())
			new AppException(SysErr.SYS_PARAM_ERR, cr.getMsg());

		int ctype = usr.getCtype();
		int valid = 0;
		List<ConstItem> list = DicConstUtils.getConst("SYS2", "ctype", false);
		for(ConstItem item: list) {
			int id = StrTool.strToInt(item.getId().toString(), 0);
			if ((id & ctype)>0) valid += id;
		}
		if (valid<=SysCorpBean.SUPPER_CORP)
			throw new AppException(SysErr.SYS_PARAM_ERR, "不支持注册此类型企业帐号.");

		usr.setCtype(valid);

		if (StrTool.isBlank(usr.getAccount()))
			throw new AppException(SysErr.SYS_PARAM_ERR, "请输入注册帐号.");
		
		if (StrTool.isBlank(usr.getPassword()))
			throw new AppException(SysErr.SYS_PARAM_ERR, "密码不可为空.");
		
		if (StrTool.isBlank(usr.getCode()))
			throw new AppException(SysErr.SYS_PARAM_ERR, "请输入验证码.");

		if (StrTool.isBlank(usr.getCorpName())) usr.setCorpName(SysCorpBean.CORPNAME_NA);

		return usr;
	}

	public boolean verify() {
		
		return true;
	}
}